﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace TrailMapManager.Communication
{
    [ServiceContract]
    public interface ITrailMapCommunication
    {
        [OperationContract]
        bool ConnectPhone(string phoneName, long maxSpace, long freeSpace);

        [OperationContract]
        TrailCommand GetNewCommand();

        [OperationContract]
        void UploadMapList(List<MapInformation> maps);

        [OperationContract]
        FileTransferSegment GetFileSegment(FileTransferSegmentRequest request);

        [OperationContract]
        void UploadTrackList(TrailTrackTransfer tracks);

        [OperationContract]
        void UploadTrackFinish();


        [OperationContract]
        void UploadPoiList(TrailPoi pois);
    }

    #region DTOs

    [DataContract]
    public class TrailCoordinate
    {
        [DataMember]
        public float Longitude { get; set; }

        [DataMember]
        public float Latitude { get; set; }
    }

    [DataContract]
    public class TrailBoundary
    {
        [DataMember]
        public TrailCoordinate NorthWest { get; set; }

        [DataMember]
        public TrailCoordinate SoutEast { get; set; }
    }

    [DataContract]
    public class MapInformation
    {
        [DataMember]
        public string FileName { get; set; }

        [DataMember]
        public string Description { get; set; }

        [DataMember]
        public TrailBoundary Boundary { get; set; }

    }

    [DataContract]
    public class TrailPoi
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public TrailCoordinate Position { get; set; }

        [DataMember]
        public string Description { get; set; }

        [DataMember]
        public Guid Id { get; set; }
    }

    [DataContract]
    public class DownloadTrailPoiCommand : TrailCommand
    {
        [DataMember]
        public TrailPoi Poi { get; set; }
    }

    [DataContract]
    public class DeleteTrailPoiCommand : TrailCommand
    {
        [DataMember]
        public TrailPoi Poi { get; set; }
    }

    [DataContract]
    [KnownType(typeof(DownloadMapCommand))]
    [KnownType(typeof(TrailTrackDeleteCommand))]
    [KnownType(typeof(DeleteTrailMapCommand))]
    [KnownType(typeof(DownloadTrailPoiCommand))]
    [KnownType(typeof(DeleteTrailPoiCommand))]
    public class TrailCommand
    {
        [DataMember]
        public CommandType Type { get; set; }
    }

    [DataContract]
    public class DeleteTrailMapCommand : TrailCommand
    {
        [DataMember]
        public MapInformation Map { get; set; }
    }

    [DataContract]
    public class DownloadMapCommand : TrailCommand
    {
        [DataMember]
        public string ServerFilePath { get; set; }

        [DataMember]
        public string FileName { get; set; }
    }



    [DataContract]
    public class FileTransferSegment
    {
        [DataMember]
        public byte[] Buffer { get; set; }
        [DataMember]
        public bool isFinish { get; set; }
        [DataMember]
        public String ServerFilePath { get; set; }
        [DataMember]
        public int Length { get; set; }
        [DataMember]
        public int StartIndex { get; set; }
        [DataMember]
        public string FileName { get; set; }
    }

    [DataContract]
    public class FileTransferSegmentRequest
    {
        [DataMember]
        public String FilePath { get; set; }
        [DataMember]
        public int StartIndex { get; set; }
        [DataMember]
        public int Length { get; set; }
        [DataMember]
        public string FileName { get; set; }
    }

    [DataContract]
    public class TrailTrackTransfer
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public TimeSpan Duration { get; set; }

        [DataMember]
        public string Description { get; set; }

        [DataMember]
        public double Distance { get; set; }

        [DataMember]
        public byte[] PointDatas { get; set; }

        [DataMember]
        public Guid Id { get; set; }
    }

    [DataContract]
    public class TrailTrackDeleteCommand : TrailCommand
    {
        [DataMember]
        public Guid Id { get; set; }
    }



    public enum CommandType
    {
        GetMapList,
        DeleteMap,
        DownloadMap,

        GetTrackList,
        DeleteTrack,
        UploadTrack,

        GetPoiList,
        DeletePoi,
        UploadPoi,
        DownloadPoi,
    }
    #endregion
}
